//
//  ScoreAlertView.swift
//  KeyboardAndEmoji
//
//  Created by AP020Mac mini on 2025/9/26.
//

import UIKit
import SnapKit

typealias StatusAndIndexBlock = (Bool, Int) -> Void

class ScoreAlertView: BaseView {
    
    
    var block: StatusAndIndexBlock?
    
    private var starButtons: [UIButton] = []
    private var selectedRating: Int = 0
    private var submitButton: UIButton!
    
    private static let APPLogoIcon = "logoIcon"
    private static let APPReviewStarFill = "review_star_fill"
    private static let APPReviewStar = "review_star"
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        configUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func configUI() {
        self.layer.cornerRadius = 15
        self.layer.masksToBounds = true
        
        let effect = UIBlurEffect(style: .systemThickMaterial)
        let effectView = UIVisualEffectView(effect: effect)
        self.insertSubview(effectView, at: 0)
        effectView.frame = self.bounds
        effectView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
        self.backgroundColor = UIColor(white: 0.8, alpha: 0.6)
        
        // App Icon
        let icon = UIImageView(image: UIImage(named: ScoreAlertView.APPLogoIcon))
        icon.layer.cornerRadius = 15
        icon.clipsToBounds = true
        self.addSubview(icon)
        icon.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: 64, height: 64))
            make.centerX.equalTo(self.snp.centerX)
            make.top.equalTo(20)
        }
        // Title Label
        let title = UILabel()
        title.text = String(format: "Do you like \"%@\"?".localizedString(), UIDevice.appName)
        title.textAlignment = .center
        title.font = UIFont.boldSystemFont(ofSize: 17)
        title.textColor = UIColor(hex: "#1D1B1B")
        self.addSubview(title)
        title.snp.makeConstraints { make in
            make.centerX.equalTo(self.snp.centerX)
            make.top.equalTo(icon.snp.bottom).offset(16)
        }
        
        // Subtitle Label
        let subTitle = UILabel()
        subTitle.text = "Tap a star to rate it in the App Store.".localizedString()
        subTitle.textAlignment = .left
        subTitle.font = UIFont.systemFont(ofSize: 14)
        subTitle.numberOfLines = 0
        subTitle.textColor = UIColor(hex: "#1D1B1B")
        self.addSubview(subTitle)
        subTitle.snp.makeConstraints { make in
            make.top.equalTo(title.snp.bottom).offset(5)
            make.left.equalTo(15)
            make.right.equalTo(-15)
        }
        
        // Star Container View
        let starView = UIView()
        self.addSubview(starView)
        starView.backgroundColor = UIColor.clear
        starView.frame = CGRect(x: 0, y: 0, width: self.frame.width, height: 44)
        starView.frame.origin.y = 178
        
        // Top line
        let line1 = UIView()
        starView.addSubview(line1)
        line1.backgroundColor = UIColor(hex: "#949494")
        line1.frame = CGRect(x: 0, y: 0, width: starView.frame.width, height: 1.0 / UIScreen.main.scale)
        
        // Bottom line
        let line2 = UIView()
        starView.addSubview(line2)
        line2.backgroundColor = UIColor(hex: "#949494")
        line2.frame = CGRect(x: 0, y: starView.frame.height, width: starView.frame.width, height: 1.0 / UIScreen.main.scale)
        
        // Create star buttons
        let starSize: CGFloat = 22
        let spacing: CGFloat = 19
        let totalWidth = 5 * starSize + 4 * spacing
        let startX = (self.frame.width - totalWidth) / 2
        
        for i in 0..<5 {
            let star = UIButton(type: .custom)
            star.frame = CGRect(x: startX + CGFloat(i) * (starSize + spacing), y: 10, width: starSize, height: starSize)
            star.setCustomBackgroundImage(KImage(ScoreAlertView.APPReviewStar))
            star.tag = i + 1
            star.addTarget(self, action: #selector(starTapped(_:)), for: .touchUpInside)
            starView.addSubview(star)
            starButtons.append(star)
            
            // Add enlarge edge (assuming this is a custom extension)
            star.contentEdgeInsets = UIEdgeInsets(top: 8, left: 8, bottom: 8, right: 8)
        }
        
        // Vertical separator line
        let line3 = UIView()
        self.addSubview(line3)
        line3.backgroundColor = UIColor(hex: "#949494")
        line3.frame = CGRect(x: 0, y: 0, width: 0.5, height: self.frame.height - starView.frame.maxY)
        line3.center.x = self.center.x
        line3.frame.origin.y = starView.frame.maxY
        
        // Cancel Button
        let cancelBtn = UIButton()
        cancelBtn.setTitle("Cancel".localizedString(), for: .normal)
        cancelBtn.setTitleColor(UIColor(hex: "#007AFF"), for: .normal)
        cancelBtn.titleLabel?.font = UIFont.systemFont(ofSize: 17)
        self.addSubview(cancelBtn)
        cancelBtn.backgroundColor = UIColor.clear
        cancelBtn.addTarget(self, action: #selector(cancelTapped), for: .touchUpInside)
        cancelBtn.snp.makeConstraints { make in
            make.left.bottom.equalTo(0)
            make.width.equalTo(self.snp.width).multipliedBy(0.5)
            make.height.equalTo(44)
        }
        
        // Submit Button
        submitButton = UIButton()
        submitButton.setTitle("Submit".localizedString(), for: .normal)
        submitButton.setTitleColor(UIColor(hex: "#808080"), for: .normal)
        submitButton.titleLabel?.font = UIFont.boldSystemFont(ofSize: 17)
        self.addSubview(submitButton)
        submitButton.backgroundColor = UIColor.clear
        submitButton.addTarget(self, action: #selector(submitTapped), for: .touchUpInside)
        submitButton.snp.makeConstraints { make in
            make.right.bottom.equalTo(0)
            make.width.equalTo(self.snp.width).multipliedBy(0.5)
            make.height.equalTo(44)
        }
    }
    
    @objc private func starTapped(_ sender: UIButton) {
        selectedRating = sender.tag
        
        for button in starButtons {
            if button.tag <= sender.tag {
                button.setCustomBackgroundImage(KImage(ScoreAlertView.APPReviewStarFill))
            } else {
                button.setCustomBackgroundImage(KImage(ScoreAlertView.APPReviewStar))
            }
        }
        
        if selectedRating > 0 {
            submitButton.setTitleColor(UIColor(hex: "#007AFF"), for: .normal)
            submitButton.setTitleColor(UIColor(hex: "#007AFF"), for: .highlighted)
        } else {
            submitButton.setTitleColor(UIColor(hex: "#808080"), for: .normal)
            submitButton.setTitleColor(UIColor(hex: "#808080"), for: .highlighted)
        }
    }
    
    @objc private func submitTapped() {
        if selectedRating <= 0 {
            return
        }
        block?(true, selectedRating)
    }
    
    @objc private func cancelTapped() {
        block?(false, 0)
    }
}

